{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-fc14b80eb044>:6: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\Program Files\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\Program Files\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Program Files\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Program Files\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "mnist = input_data.read_data_sets(\"MNIST_data/\", reshape=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X_test:  (10000, 48, 48, 1)\n",
      "tmp:  (1440, 1440)\n"
     ]
    }
   ],
   "source": [
    "import png\n",
    "import math\n",
    "\n",
    "\n",
    "padding = 10\n",
    "paded = 28 + padding*2\n",
    "n = 900\n",
    "n2 = int(math.sqrt(n))\n",
    "\n",
    "X_test = mnist.test.images\n",
    "X_test = np.pad(X_test, ((0,0),(padding,padding),(padding,padding),(0,0)), 'constant')\n",
    "\n",
    "print (\"X_test: \", X_test.shape)\n",
    "  \n",
    "\n",
    "tmp = np.zeros((paded*n2, paded*n2), dtype=np.float)\n",
    "\n",
    "for i in range(n2):\n",
    "    for j in range(n2):\n",
    "        for row in range(paded):\n",
    "            for col in range(paded):\n",
    "                tmp[paded*i+ row][paded*j + col]  = X_test[n2*i+j][row][col]\n",
    "    \n",
    "print (\"tmp: \", tmp.shape)\n",
    "\n",
    "output_dirs = \"./\"\n",
    "\n",
    "output_file = os.path.join(output_dirs, \"test_\"+str(n)+\".png\")\n",
    "with open(output_file, \"wb\") as f:\n",
    "    w = png.Writer(paded*n2, paded*n2, greyscale=True)\n",
    "    w.write(f,(tmp*255).astype(int))\n",
    "        \n",
    "        \n",
    "#print((mnist.train.images[2].reshape(28, 28)*255).astype(int))\n",
    "#for i in range(10):\n",
    "#    output_file = os.path.join(output_dirs, str(i)+\".png\")\n",
    "#    with open(output_file, \"wb\") as f:\n",
    "#        w = png.Writer(paded, paded, greyscale=True)\n",
    "#        w.write(f,(X_test[0:n].reshape(paded*n/2, paded*n/2)*255).astype(int))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
